package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.BusinessInformEntity;
import com.qc.entity.MerchantEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.BusinessInformService;
import com.qc.service.MerchantService;

import net.sf.json.JSONObject;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebMerchantController {

	@Resource(name="merchantService")
	private MerchantService merchantService;
	
	@Resource(name="businessInformService")
	private BusinessInformService businessInformService;
	
	
	/**
     * 跳转到首页
     * @return
     */
    @RequestMapping(value = "/toWebMerchantIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toWebMerchantIndex() {
        ModelAndView mav = new ModelAndView("/shop/shop_list");
        
        return mav;
    }
    
    /**
     * 数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllMerchant", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllMerchant(HttpServletRequest request) {
        
        String shopName = request.getParameter("name");
        int isAuth = Integer.parseInt(request.getParameter("isAuth"));
        
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = merchantService.getWebListMerchantCount(isAuth, shopName);
        
        //根据条件查询当前页数据
        List<MerchantEntity> list = merchantService.getWebAllMerchant(isAuth, shopName, pageNo, pageSize);
        
        //分页查询公共类
        Page<MerchantEntity> page = new Page<MerchantEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<MerchantEntity> pageModel = new PageModelList<MerchantEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 修改
     */
    
    @RequestMapping(value = "/toUpdateIsAuthId")
    @ResponseBody
    public int toUpdateIsAuthId(HttpServletRequest request, HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int isAuth = Integer.parseInt(request.getParameter("isAuth"));
        String refuse = request.getParameter("refuse");
        
        int flag = merchantService.updateIsAuth(id, isAuth,refuse);
        
        return flag;
    }
    
    /**
     * 详细页
     * @param request
     * @return
     */
    @RequestMapping(value = "/toWebMerchantform", produces = "text/html;charset=UTF-8")
    public ModelAndView toWebMerchantform(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/shop/shop_inform");
        long id = Long.parseLong(request.getParameter("id"));
        
        MerchantEntity merchantEntity = merchantService.getMerchantId(id);
        
        mav.addObject("merchantEntity",merchantEntity);
        
        return mav;
    }
    
    /**
     * 删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteWebMerchantId")
    @ResponseBody
    public int toDeleteWebMerchantId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = merchantService.deleteMerchantId(id);
        return flag;
    }
    

	/**
     * 查看商家发布页
     * @return
     */
    @RequestMapping(value = "/toWebSaveMerchantIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toWebSaveMerchantIndex() {
        ModelAndView mav = new ModelAndView("/shop/shop_fabu");
        
        return mav;
    }
    
    /**
     * 商家发布 数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllBusinssInfo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllBusinssInfo(HttpServletRequest request) {
        
        String shopName = request.getParameter("name");
        String time = request.getParameter("time");
        
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = businessInformService.getListWebBusinessInformCount(shopName, time);
        
        //根据条件查询当前页数据
        List<BusinessInformEntity> list = businessInformService.getAllWebBusinessInform(shopName, time, pageNo, pageSize);
        
        //分页查询公共类
        Page<BusinessInformEntity> page = new Page<BusinessInformEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<BusinessInformEntity> pageModel = new PageModelList<BusinessInformEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 商家发布 详细页
     * @param request
     * @return
     */
    @RequestMapping(value = "/toWebBusinssInfo", produces = "text/html;charset=UTF-8")
    public ModelAndView toWebBusinssInfo(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/shop/shop_fabuinf");
        String id = request.getParameter("id");
        
        BusinessInformEntity informEntity = businessInformService.getWebBusinessInformId(id);
        
        mav.addObject("informEntity",informEntity);
        
        return mav;
    }
    
    /**
     * 商家发布 删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteWebBusinssInfo")
    @ResponseBody
    public int toDeleteWebBusinssInfo(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        
        int flag = businessInformService.deleteBusinessInformId(id);
        
        return flag;
    }
    
    
	
}
